import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from '@vant/auto-import-resolver'
import UnoCSS from 'unocss/vite'
import { resolve } from 'path'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    UnoCSS(),
    AutoImport({
      resolvers: [VantResolver()],
    }),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  resolve: {
    alias: {
      // 配置路径别名
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      '@com': resolve(__dirname, 'src/components'),
    },
    // 配置省略后缀名
    extensions: ['.vue', '.js', '.json'],
  },
  // 配置代理跨域请求
  server: {
    proxy: {
      '/server': {
        target: 'http://127.0.0.1:4000', // 后端接口
        changeOrigin: true,
        // 将 /dev-api 重写 ''
        rewrite: (path) => path.replace(/^\/server/, ''),
      },
    },
  },
})
